package com.oocl.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.oocl.db.FoodTypeDB;
import com.oocl.factory.ServiceFactory;
import com.oocl.log4j.Log4j;
import com.oocl.pojo.Food;
import com.oocl.pojo.Merchant;
import com.oocl.service.FoodService;
import com.oocl.service.impl.FoodServiceImpl;
import com.oocl.util.ObjConvertUtil;

/**
 * Servlet implementation class ShowSpecificFoodServlet
 */
public class ShowSpecificFoodServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private FoodService foodService;   

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		foodService = ServiceFactory.getFoodService();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		/*String typeId = request.getParameter("typeId");
		HttpSession session = request.getSession();
		Merchant merchant = (Merchant) session.getAttribute("merchant");
		
		Log4j.getLogger().info("当前 typeId 为： {}", typeId);
		Log4j.getLogger().info("当前 merchantId 为： {}", merchant.getId());
		
		List<Food> foods = new ArrayList<Food>();
		if(typeId.equals("allType")){
			foods = foodService.getFoodByMerchantId(merchant.getId());
		}else{
			foods = foodService.findByTypeIdAndMerchantId(typeId, merchant.getId());
		}
		
		List<FoodVO> foodVOs = ObjConvertUtil.FoodDOsToFoodVOs(foods);
		
		Log4j.getLogger().info("根据 typeId, merchantId 从数据库中拿到的 Foods 为： {}", foods.toString());
		
		String currentType = FoodTypeDB.getFoodTypes().get(typeId);
		if(currentType == null){
			currentType = "allType";
		}
		request.setAttribute("currentType", currentType);
		request.setAttribute("foodVOs", foodVOs);
		if(foodVOs.size() == 0){
			
			request.setAttribute("warning", "请及时添加商品信息哦");
		}
		request.getRequestDispatcher("view/showFoodByType.jsp").forward(request, response);*/
	}

}
